<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\CustomServiceStoreRequest;
use App\Http\Requests\CustomServiceUpdateRequest;

use App\Models\CustomService;
use Auth;

class CustomServiceController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $columns = ['客户','服务内容','类型','状态','创建人','创建时间','客户经理'];
        return $this->index_model_list(CustomService::class,$columns,$request,function($builder) use($request){

            $keyword = $request->get('keyword','');
            $status = $request->get('status',0);

            $with = ['type'];
            if($status == 0){
                $with[] = 'user';
            }elseif($status == 1 || $status ==2 || $status == 3){
                $with[] = 'user';
                $with[] = 'assignUser';
            }

            $builder->with($with);
            $builder->where(['status'=>$status]);

            //自定义查询条件
            if(!empty($keyword)){
                $builder->where('custom_name','like','%'.$keyword.'%')
                        ->orWhere('content','like','%'.$keyword.'%');
            }
        },function($model) {
            //导出字段定义
            return [
               $model->custom_name,
               $model->content,
               $model->type->value,
               $model->getStatusText(),
               $model->user->name,
               $model->created_at,
               $model->assignUser != null ? $model->assignUser->name : ''
           ];
        });
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CustomServiceStoreRequest $request)
    {
        $data = array_null_to_empty($request->only([
            'custom_name','service_type','content','remark'
        ]));

        $model = new CustomService();
        $model->user_id = Auth::user()->id;
        $model->status = 0;
        $model->fill($data);
        $model->save();
        return response()->json(json_success(['id'=>$model->id]));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = CustomService::findOrFail($id);
        return response()->json(json_success($model));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CustomServiceUpdateRequest $request, $id)
    {
        $data = array_filter($request->only([
            'custom_name','service_type','content','remark'
        ]));

        $model = CustomService::findOrFail($id);
        $model->update($data);
        return response()->json(json_success());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = CustomService::findOrFail($id);
        $model->delete();
        return response()->json(json_success());
    }
}
